package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.domain.StrategyTheme;
import cn.wolfcode.luowowo.query.QueryObject;
import cn.wolfcode.luowowo.query.RegionQuery;
import cn.wolfcode.luowowo.repository.StrategyThemeRepository;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyThemeServiceImpl implements IStrategyThemeService {

    @Autowired
    private StrategyThemeRepository strategyThemeRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void insert(StrategyTheme strategyTheme) {
        strategyTheme.setId(null);
        strategyThemeRepository.save(strategyTheme);
    }

    @Override
    public void update(StrategyTheme strategyTheme) {
        strategyThemeRepository.save(strategyTheme);
    }

    @Override
    public void delete(String id) {
        strategyThemeRepository.deleteById(id);
    }

    @Override
    public StrategyTheme select(String id) {
        return strategyThemeRepository.findById(id).get();
    }

    @Override
    public Page<StrategyTheme> query(QueryObject qo) {
        Query query = new Query();

        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1,qo.getPageSize()
                , Sort.Direction.ASC, "_id");


        return DBHelper.query(mongoTemplate, StrategyTheme.class, query, pageable);
    }

    @Override
    public List<StrategyTheme> listAll() {
        return strategyThemeRepository.findAll();
    }
}
